package com.example.systemend.service.GoodsImpl;


import com.example.systemend.Mapper.GoodsMapper;
import com.example.systemend.pojo.App;
import com.example.systemend.pojo.Goods;
import com.example.systemend.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 权哥
 * @version 1.0
 * @date 2022/5/31 20:49
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    //库存
    @Override
    public List<Goods> listinventory() {
        System.out.println("库存查询成功！！！");
        return goodsMapper.select_inventory();
    }
    //货物添加
    @Override
    public void purchase_add(Map map) {
        Goods goods=new Goods();
        //订单编号实例
        OrderNum orderNum=new OrderNum();
        //1.商品名称
        goods.setGoods_name((String) map.get("goods_name"));
        //2.商品编号
        goods.setGoods_num(Integer.parseInt(map.get("goods_num").toString()));
        //3.货物类型
        goods.setCategory((String) map.get("category"));
        //4.进货数量
        goods.setPurchase_quantity(Integer.parseInt(map.get("purchase_quantity").toString()));
        //5.进货价格
        goods.setPurchase_price(Double.parseDouble((String) map.get("purchase_price")));
        //6.进货负责人
        goods.setPurchase_responsible((String) map.get("purchase_responsible"));
        //7.进货供应商
        goods.setPurchase_supplier((String) map.get("purchase_supplier"));
        //8.进货订单号
        goods.setPurchase_order_num(orderNum.date()+orderNum.makeNum(5,99999));
        //9.进货时间
        goods.setPurchase_date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        //进货订单更新
        goodsMapper.purchase_insert(goods);
        //查询库存是否存在
        Goods search=goodsMapper.search_inventory(goods.getGoods_num());
        System.out.println("==========================================");
        //若进购货物编号库存不存在，插入数据，否则更新库存数量
        if(search.getGoods_num()==-1){
            System.out.println("进行数据添加===============>");
            goodsMapper.insert_inventory(goods);
            System.out.println("货物编号："+goods.getGoods_num()+"=====>添加成功");
            System.out.println("库存更新成功！！！");
        }else{
            System.out.println("进行数据修改===============>");
            goodsMapper.update_inventory(goods);
            System.out.println("货物编号："+goods.getGoods_num()+"=====>更新成功");
            System.out.println("库存更新成功！！！");
        }
    }

    //所有进货订单
    @Override
    public List<Goods> select_purchase() {
        System.out.println("==========================================");
        System.out.println("进货订单查询成功!!!");
        return goodsMapper.select_purchase();
    }

    //所有出货订单
    @Override
    public List<Goods> select_shipment() {
        System.out.println("==========================================");
        System.out.println("出货订单查询成功!!!");
        return goodsMapper.select_shipment();
    }
    //出货订单添加
    @Override
    public void shipment_add(Map map) {
        Goods goods=new Goods();
        //订单编号实例
        OrderNum orderNum=new OrderNum();
        //1.订单编号
        goods.setShipment_order_num(orderNum.date()+orderNum.makeNum(6,999999));
        //2.出货数量
        goods.setShipment_quantity(Integer.parseInt(map.get("shipment_quantity").toString()) );
        //3.货物编号
        goods.setGoods_num(Integer.parseInt( map.get("goods_num").toString()));
        //4.出货价格
        goods.setShipment_price(Double.parseDouble(map.get("shipment_price").toString()));
        //5.出货时间
        goods.setShipment_date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //6.负责人
        goods.setShipment_responsible( map.get("shipment_responsible").toString());
        //7.出货店商
        goods.setShipment_shops( map.get("shipment_shops").toString());
        System.out.println("==========================================");
        System.out.println("店铺为：===>"+map.get("shipment_shops"));

        //查询库存是否存在
        Goods search=goodsMapper.search_inventory(goods.getGoods_num());
        //若该销售产品编号存在，则进行下一步操作
        if(search.getGoods_num()!=-1)
        {
            Goods a_goods=goodsMapper.a_search_inventory(goods.getGoods_num());
            //当销售量小于等于库存量时
            if(goods.getShipment_quantity()<=a_goods.getQuantity())
            {
                goodsMapper.shipment_insert(goods);
                System.out.println("出货订单添加成功！！！");
                //改变数字符号
                goods.setPurchase_quantity(-goods.getShipment_quantity());
                goodsMapper.update_inventory(goods);
                System.out.println("货物编号："+goods.getGoods_num()+"=====>更新成功");
                System.out.println("库存更新成功！！！");
            }else {
                System.out.println("库存不足!!!");
            }

        }else {
            System.out.println("库存不存在！！！");
        }


    }

    //主页数据
    @Override
    public List<App> getResult() {
        List<App> list=new ArrayList<>();
        App app=new App();
        //1、今日进货量
        app.setToday_purchase_quantity(goodsMapper.today_purchase_quantity(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
        //2、今日出货量
        app.setToday_shipment_quantity(goodsMapper.today_shipment_quantity(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
        //3、今日流水
        app.setToday_sales_money(goodsMapper.today_sales_money(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
        //4、今日毛利润
        app.setToday_profit(goodsMapper.today_profit(new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
        list.add(app);
        System.out.println("==========================================");
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"主页更新成功！！！");
        System.out.println("主页数据===>"+"进货量："+app.getToday_purchase_quantity()+
                                        "  销量："+app.getToday_shipment_quantity()+
                                        "  销售额："+app.getToday_sales_money()+
                                        "  毛利润："+app.getToday_profit());
        return list;
    }

    @Override
    public List<Goods> getInventory(Map map) {
        String category;
        category=map.get("category").toString();
        System.out.println("类型为："+category);
        System.out.println(goodsMapper.getInventory(category));
        return goodsMapper.getInventory(category);
    }


}
